//
//  SearchClassShowView.m
//  AmountBody
//
//  Created by smm on 2018/1/20.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "SearchClassShowView.h"
#import "ClassModel.h"
#define Button_Tag 80000
@implementation SearchClassShowView
-(instancetype)init
{
    self = [super init];
    if (self) {
        [self initSubViews];
    }
    return self;
}

-(void)initSubViews
{
    _backButton = [[UIButton alloc]init];
    [self addSubview:_backButton];
    _searchView = [[UIView alloc]init];
    [self addSubview:_searchView];
    _searchText = [[UITextField alloc]init];
    [_searchView addSubview:_searchText];
    _searchImage = [[UIImageView alloc]init];
    [_searchView addSubview:_searchImage];
    _classView = [[UIView alloc]init];
    [self addSubview:_classView];
    _classImage = [[UIImageView alloc]init];
    [_classView addSubview:_classImage];
    _classScroll = [[UIScrollView alloc]init];
    [_classView addSubview:_classScroll];
    
}

-(void)loadSearchViewWithClassArray:(NSArray*)classArr andHasBack:(BOOL)hasBack andFirstChoose:(int)index
{
    self.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 20;
    CGFloat buttonW = 64;
    CGFloat buttonH = 44;
    CGFloat viewJGW = 120;
    CGFloat s_imageW = 20;
    CGFloat s_imageH = 20;
    if (hasBack) {
        _backButton.hidden = NO;
        _classView.hidden = YES;
    }else{
        _backButton.hidden = YES;
        _classView.hidden = NO;
    }
    _index = index;
    _classArray = classArr;
    [_backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.equalTo(@(jianGeH));
        make.width.equalTo(@(buttonW));
        make.height.equalTo(@(buttonH));
        
    }];
    //    _backButton.frame = CGRectMake(5, jianGeH, buttonW, buttonH);
    [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateSelected];
    [_backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    _backButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_searchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(viewJGW));
        make.top.equalTo(@(jianGeH));
        make.right.equalTo(@(-viewJGW));
        make.height.equalTo(@(44));
    }];
    //    _searchView.clipsToBounds=YES;//不显示超出父控件部分
    _searchView.backgroundColor =RGBColorAlpha(255, 255, 255, 1);
    [_searchView setYuanJiaoForRadius:10.0 andMasks:NO];
    _searchText.textAlignment = NSTextAlignmentCenter;
    [_searchText mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10*2+s_imageW));
        make.top.equalTo(@(0));
        make.right.equalTo(@(-(10*2+s_imageW)));
        make.height.equalTo(@(44));
    }];
    
    _searchText.tag = 50001;
    _searchText.placeholder = GDLocalizedString(@"search_placeholder");
    _searchText.font = Cell_Big_Font;
    _searchText.returnKeyType = UIReturnKeySearch;
    _searchText.delegate = self;
    [_searchImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@((44-s_imageH)/2.0));
        make.right.equalTo(@(-10));
        make.width.equalTo(@(s_imageW));
        make.height.equalTo(@(s_imageH));
    }];
    _searchImage.image = [UIImage imageNamed:@"search2"];
    CGFloat c_imageW = 19;
    CGFloat c_imageH = 18;
    [_classView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_searchView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.equalTo(@(60));
    }];
    
    _classView.backgroundColor = [UIColor clearColor];
    [_classImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@((60-c_imageH)/2.0));
        make.left.equalTo(@(jianGeW));
        make.width.equalTo(@(c_imageW));
        make.height.equalTo(@(c_imageH));
    }];
    
    _classImage.image = [UIImage imageNamed:@"@2Rectangle"];
    [_classScroll mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_classView).offset(15);
        make.height.equalTo(@(40));
        make.left.equalTo(_classImage.mas_right);
        make.right.equalTo(_classView);
    }];
    [self loadScrollMasonry];
}


-(void)loadScrollMasonry
{
    for (UIView* view in _classScroll.subviews) {
        [view removeFromSuperview];
    }
    if (_classArray.count <= 0) {
        _classScroll.hidden = YES;
        return;
    }else{
        _classScroll.hidden  =NO;
    }
    UIButton * l_button = nil;
    for (int i=0; i<_classArray.count; i++) {
        ClassModel * model = [_classArray objectAtIndex:i];
        //        width = inButton.frame.origin.x+inButton.frame.size.width;
        UIButton * button = [[UIButton alloc]init];
        [button setTitle:model.name forState:UIControlStateNormal];
        button.titleLabel.font = Cell_Big_Font;
        if (i == _index) {
            [button  setTitleColor:RGBColorAlpha(253, 66, 89, 1) forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        
        [button sizeToFit];
        [_classScroll addSubview:button];
        [button mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_classScroll);
            if (l_button!=nil) {
                make.left.equalTo(l_button.mas_right);
            }else{
                make.left.equalTo(_classScroll);
            }
            
            make.width.equalTo(@(button.frame.size.width+20));
        }];
        
        //        button.frame = CGRectMake(0+width, 0, button.frame.size.width+20, viewH);
        button.tag = Button_Tag+i;
        [button addTarget:self action:@selector(classFunc:) forControlEvents:UIControlEventTouchUpInside];
        l_button = button;
    }
    [l_button mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_classScroll);
        
    }];
}

#pragma mark - textFeildDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_searchBlock) {
        _searchBlock(textField.text,YES);
    }
    [textField resignFirstResponder];
    //    _searchView.backgroundColor = RGBColorAlpha(155, 155, 155, 1);
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //    _searchView.backgroundColor = [UIColor whiteColor];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    
    return YES;
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    //    _searchView.backgroundColor = RGBColorAlpha(155, 155, 155, 1);
    return YES;
}

#pragma mark -  Block
static BackButtonBlock _backBlock;
+(void)blockForBackButton:(BackButtonBlock)backBlock
{
    _backBlock  = backBlock;
    
}
static SearchButtonBlock _searchBlock;
+(void)blockForSearchButton:(SearchButtonBlock)searchBlock
{
    _searchBlock = searchBlock;
}
static SearchClassBlock _classBlock;
+(void)blockForSearchClass:(SearchClassBlock)classBlock
{
    _classBlock = classBlock;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
#pragma mark -  手势按钮方法
-(void)classFunc:(UIButton*)tmpButton
{
    
    for (int i=0; i<_classArray.count; i++) {
        UIButton * button = (UIButton*)[_classScroll viewWithTag:Button_Tag+i];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    [tmpButton  setTitleColor:RGBColorAlpha(253, 66, 89, 1) forState:UIControlStateNormal];
    int index = (int)tmpButton.tag-Button_Tag;
    ClassModel * model = [_classArray objectAtIndex:index];
    if (_classBlock) {
        _classBlock(index);//class_id
    }
}
- (void)backButton:(UIButton *)sender {
    if (_backBlock) {
        _backBlock();
    }
}

@end
